/**
 * Created by wanghuan on 2017/4/9.
 */

/*==========================Company 相关接口==============================*/

import Ajax from '../../apiAjax';

export  default {
  state:{
    orgTreeList:[],//组织树
    orgFrom:{}
  },
  mutations:{
    setCommonStateData(state,{key,data}){
      state[key] = data;
    },
    addAddressToOrgForm(state,data){
      state.orgFrom.companyAddressList.push(data);
    },
  },
  actions:{
    /**
     *查询组织树
     * @param commit
     * @param paramData
     * @returns {Promise}
     */
    getOrgTreeList({commit},paramData){
      return new Promise((resolve, reject) => {
        Ajax({
          url:'/org/orgTreeList',
          data:paramData,
          success:function (data) {
            commit('setCommonStateData',{key:'orgTreeList',data:data.data});
            resolve(data);
          }
        })
      })
    },
    getOrgForm({commit},paramData){
      return new Promise((resolve, reject) => {
        Ajax({
          url:'/org/orgFrom',
          data:paramData,
          success:function (data) {
            commit('setCommonStateData',{key:'orgFrom',data:data.data});
            resolve(data);
          }
        })
      })
    },
    /**
     * 新增工作地址
     * @param commit
     * @param paramData
     * @returns {Promise}
     */
    saveAddress({commit},paramData){
      return new Promise((resolve, reject) => {
        Ajax({
          url:'/company/saveAddress',
          data:paramData,
          success:function (data) {
            commit('addAddressToOrgForm',data.data);
            resolve(data);
          }
        })
      })
    },
    /**
     * 新增组织
     * @param commit
     * @param paramData
     * @returns {Promise}
     */
    saveOrg({commit},paramData){
      return new Promise((resolve, reject) => {
        Ajax({
          url:'/org/saveOrg',
          data:paramData,
          success:function (data) {
            resolve(data);
          }
        })
      })
    },
},
  getters: {
    orgTreeList:(state, getters)=> {
      return state.orgTreeList;
    },
    orgFrom:(state, getters)=> {
      return state.orgFrom;
    }
  }
}
